package com.tunis.gtug.client;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.Direction;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.fx.FxConfig;
import com.extjs.gxt.ui.client.util.Rectangle;
import com.extjs.gxt.ui.client.widget.Composite;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.ProgressBar;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;

public class Tasks extends LayoutContainer {
	private FormData formData;
	private boolean ok=true;
	public transient Developper dev;

	public Tasks() {
	}

	protected void onRender(final Element parent, int pos) {
		super.onRender(parent, pos);
		formData = new FormData("-20");
		 
		
		final FormPanel form2 = new FormPanel();
		form2.setFrame(true);
		form2.setHeading("Personnal Informations");
		form2.setWidth(300);
		form2.setLayout(new FlowLayout());

		FieldSet fieldSet = new FieldSet();
		fieldSet.setHeading("User Information");
		// fieldSet.setCheckboxToggle(true);

		FormLayout layout = new FormLayout();
		layout.setLabelWidth(65);
		fieldSet.setLayout(layout);

		final TextField<String> firstName = new TextField<String>();
		firstName.setFieldLabel("First Name");
		firstName.setAllowBlank(false);
		fieldSet.add(firstName, formData);

		final TextField<String> lastName = new TextField<String>();
		lastName.setFieldLabel("Last Name");
		fieldSet.add(lastName, formData);

		final TextField<String> company = new TextField<String>();
		company.setFieldLabel("Company");
		fieldSet.add(company, formData);

		final TextField<String> email = new TextField<String>();
		email.setFieldLabel("Email");
		fieldSet.add(email, formData);

		form2.add(fieldSet);

		fieldSet = new FieldSet();
		fieldSet.setHeading("Phone Numbers");
		fieldSet.setCollapsible(true);

		layout = new FormLayout();
		layout.setLabelWidth(65);
		fieldSet.setLayout(layout);

		TextField<String> field = new TextField<String>();
		field.setFieldLabel("Home");
		fieldSet.add(field, formData);

		field = new TextField<String>();
		field.setFieldLabel("Business");
		fieldSet.add(field, formData);

		field = new TextField<String>();
		field.setFieldLabel("Mobile");
		fieldSet.add(field, formData);

		field = new TextField<String>();
		field.setFieldLabel("Fax");
		fieldSet.add(field, formData);

		form2.add(fieldSet);
		form2.setButtonAlign(HorizontalAlignment.CENTER);
		Button b = new Button("Save");
		form2.addButton(b);
		form2.addButton(new Button("Cancel"));

		b.addListener(Events.OnClick, new Listener() {

			@Override
			public void handleEvent(BaseEvent be) {
//				//Window.alert("Hello ! It's just an example. We 're waiting subscription on Google SQL Cloud :D");
//				Dialog dialog = new Dialog();
//				dialog.setStyleName("window3");
//				dialog.addText("Hello ! It's just an example. We 're waiting subscription on Google SQL Cloud :D");
//				dialog.show();
				final MessageBox box = MessageBox.progress("Please wait", "Saving items...",  
			            "Initializing...");  
			        final ProgressBar bar = box.getProgressBar();  
			        final Timer t = new Timer() {  
			          float i;  
			 
			          @Override  
			          public void run() {  
			            bar.updateProgress(i / 100, (int) i + "% Complete");  
			            i += 40;  
			            if (i > 120) {  
			              cancel();  
			              box.close();  
			              
			             
			              form2.reset();
			              //form2.el().fadeToggle(FxConfig.NONE);
			              form2.el().fadeToggle(new FxConfig(2500));
			             
			              ok=false;
							
			          
			            
			            
			              
			              
			             
			              //form3.el().fadeToggle(FxConfig.NONE);  
//			              Rectangle rect =  .el().getBounds();  
//			              parent.el().setXY(rect.x + 50, rect.y + 50, FxConfig.NONE);  
			              
			            }
			            
			            	} 
			            
			        };  
			        t.scheduleRepeating(500);  
			        String prenom = firstName.getValue();
			        String nom = lastName.getValue();
			        String mail = email.getValue();
			        String occupation = company.getValue();
			        if (!nom.equals("")||!mail.equals("")||!occupation.equals("")){
			        dev=new Developper(prenom,nom,mail,occupation);
			        Info.display("Message", "Updated member !", "");
			        }
			        else Info.display("Message", "Please fill elements","");

			        Tunis_gtug.grid.models.add(dev);
			        Tunis_gtug.grid.store.insert(dev,0);
			        Tunis_gtug.greetingService.greetServer(prenom, nom, occupation, mail, new AsyncCallback<String>() {

						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							Info.display("Message", "DataBase connection failed");
						}

						@Override
						public void onSuccess(String result) {
							// TODO Auto-generated method stub
							Info.display("Message", "Updated Into Data Base");
						}
					});
			        
			        

			      }  
			   
			
		});
		
		add(form2);
		
	
		

	}
}
